package demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {
    //验证枚举类能否被反射
    public static void reflectPrivateConstructor(){
        try {
            Class<?> c1 = Class.forName("demo.TestNum");
//            Constructor<?> constructor = c1.getDeclaredConstructor(double.class, String.class);//这里会报错
            //因为创建的枚举类继承于父类Enum,父类的Enum中构造方法里包含两个参数（String,int），所以子类会棒父类构造，会构造父类中默认存在的这两个参数，故：
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class, int.class, double.class, String.class);
            //此时在实例化TestNum的时候也需要传入相应的参数
            constructor.setAccessible(true);
//            TestNum testNum = (TestNum)constructor.newInstance(8.8, "青色");
            TestNum testNum = (TestNum)constructor.newInstance("123", 8, 8.8, "青色");//会报错
            //因为枚举不允许通过反射的方式创建对象啊

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }

    public static void main1(String[] args) {
        Constant constant = new Constant();
        System.out.println(Constant.BLACK);
    }
}
